******************************************************************
*This file cleans the tuition fees of institutions from the IPEDS*
******************************************************************

*** create dta files from csv, labeling
cd "$raw_data_education/tuitionFees"

foreach i in 1989 {
cap do "$do_clean_education/IPEDS - do/do tuitionFees/ic`i'_d.do"
keep unitid ciptuit1 ciptuit2 ciptuit3
egen avgCost = rowmean(ciptuit1 ciptuit2 ciptuit3)
label variable avgCost "average tuition fee for the 3 top CIP codes by enrollment"
drop ciptuit1 ciptuit2 ciptuit3 
gen year = `i'
save "$clean_data_education/tuitionFees_`i'.dta", replace
clear
}
**************************************

foreach i in 1990 1991 1993 1994 1995 1999 2006 2007 2008 2013 2014 2015 {
cap do "$do_clean_education/IPEDS - do/do tuitionFees/ic`i'_py.do"
keep unitid ciptuit1 ciptuit2 ciptuit3 ciptuit4 ciptuit5 ciptuit6
egen avgCost = rowmean(ciptuit1 ciptuit2 ciptuit3 ciptuit4 ciptuit5 ciptuit6)
label variable avgCost "average tuition fee for the 6 top CIP codes by enrollment"
drop ciptuit1 ciptuit2 ciptuit3 ciptuit4 ciptuit5 ciptuit6
gen year = `i' 
save "$clean_data_education/tuitionFees_`i'.dta", replace
clear
}
**************************************

foreach i in 2000 2001 {
cap do "$do_clean_education/IPEDS - do/do tuitionFees/ic`i'_py.do"
keep unitid chg1py3 ciptuit2 ciptuit3 ciptuit4 ciptuit5 ciptuit6
egen avgCost = rowmean(chg1py3 ciptuit2 ciptuit3 ciptuit4 ciptuit5 ciptuit6)
label variable avgCost "average tuition fee for the 6 top CIP codes by enrollment"
drop chg1py3 ciptuit2 ciptuit3 ciptuit4 ciptuit5 ciptuit6 
gen year = `i'
save "$clean_data_education/tuitionFees_`i'.dta", replace
clear
}
**************************************

* append
use "$clean_data_education/tuitionFees_1989.dta", clear
foreach i in 1990 1991 1993 1994 1995 1999 2000 2001 2006 2007 2008 2013 2014 2015 {
append using "$clean_data_education/tuitionFees_`i'.dta"
rm "$clean_data_education/tuitionFees_`i'.dta"
}
rm "$clean_data_education/tuitionFees_1989.dta"

* final cleanings
do "$do_clean_education/clean institutions - id.do"

drop if avgCost == . // info not available 
drop if avgCost == 0 // impossible, then info not available

*** Build three year averages
* year group
gen yearGroup = 0
replace yearGroup = 1990 if year <= 1991 
replace yearGroup = 1994 if year >= 1993 & year <= 1995
replace yearGroup = 2000 if year >= 1999 & year <= 2001
replace yearGroup = 2007 if year >= 2006 & year <= 2008
replace yearGroup = 2014 if year >= 2013

**********************************************************
* Run it all together, save labels and collapse
foreach v of var * {
local l`v' : variable label `v'
	if `"`l`v''"' == "" {
	local l`v' "`v'"
  	}
}
collapse (mean) avgCost, by(unitid yearGroup)

foreach v of var * {
label var `v' "`l`v''"
}
**********************************************************

save "$clean_data_education/tuitionFees.dta", replace
